//
//  YFWeightAlertPickerView.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/17.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFWeightAlertPickerView.h"
#import "YFUserSeriesViewModel.h"

@interface YFWeightAlertPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *titlelabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@property (nonatomic, strong) NSArray *lbsArr;

/** 第一列默认显示的文字 */
@property (nonatomic, copy)NSString *firstColValue;
@property (nonatomic, copy)NSString *currentLbsStr;


@end

@implementation YFWeightAlertPickerView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self clipsCornerRadius:6 borderWidth:1.5 borderColor:kYellowGreenColor];
    [self.confirmButton clipsCornerRadius:3];
}

- (IBAction)confirmButtonClick:(id)sender {
    if (self.selectCallback) {
        self.selectCallback(self.currentLbsStr);
    }
}

+ (instancetype)weightAlertPickerViewWithWeight:(NSString *)weight {
     YFWeightAlertPickerView *view = [[NSBundle mainBundle] loadNibNamed:@"YFWeightAlertPickerView" owner:nil options:nil].lastObject;
    view.firstColValue = weight;
    view.currentLbsStr = weight;
    return view;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if ([self.lbsArr containsObject:self.firstColValue]) {
        NSInteger oneIndex = [self.lbsArr indexOfObject:self.firstColValue];
        [self.pickerView selectRow:oneIndex inComponent:0 animated:NO];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.lbsArr.count;
}

- (NSArray *)lbsArr {
    return [YFUserSeriesViewModel shareInstance].weights;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        UIFont *font = kOpenSansFont(OpenSansExtraBold, 20);
        pickerLabel.font = font;
        pickerLabel.textColor = kTWhiteText;
    }
    NSString *title = @"";
    if (component == 0) {
        title = self.lbsArr[row];
    }
    pickerLabel.text = title;
    [pickerLabel sizeToFit];
    return pickerLabel;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 48;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.currentLbsStr = self.lbsArr[row];
    }
}



@end
